package com.penghaonan.appframework.utils.listview;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.penghaonan.appframework.utils.threadpool.AThreadPool;
import com.penghaonan.appframework.utils.threadpool.IThreadPoolHolder;

abstract public class BaseItemView<T> extends FrameLayout implements CompoundButton.OnCheckedChangeListener {

    private int index = -1;
    private T data;
    private IItemCheckListener onCheckedChangeListener;
    protected ItemAdapter<T> adapter;

    public BaseItemView(Context context) {
        super(context);
        init();
    }

    public BaseItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (getLayoutRes() != 0) {
            inflate(getContext(), getLayoutRes(), this);
            initViews();
        }
    }

    public T getData() {
        return data;
    }

    protected void onInit(@NonNull Bundle env){}

    abstract protected int getLayoutRes();

    abstract protected void initViews();

    public void setData(T data, AThreadPool threadPool) {
        T oldData = this.data;
        this.data = data;
        boolean needAsync = onBindData(oldData, data);

        if (needAsync) {
            if (threadPool == null) {
                if (getContext() instanceof IThreadPoolHolder) {
                    threadPool = ((IThreadPoolHolder) getContext()).getIOThreadPool();
                }
            }
            if (threadPool != null) {
                threadPool.addTask(new ItemAdapter.BindTask(BaseItemView.this, data));
            }
        }
    }

    public void setData(T data, int index) {
        this.index = index;
        setData(data, null);
    }

    public void setData(T data, AThreadPool threadPool, int index) {
        this.index = index;
        setData(data, threadPool);
    }

    public void setData(T data) {
        setData(data, null);
    }

    public void setOnCheckedChangeListener(IItemCheckListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public void setAdapter(ItemAdapter<T> adapter) {
        this.adapter = adapter;
    }

    /**
     * 绑定数据
     *
     * @return true:启用异步绑定,在asyncBindData中完成   false:不启用异步绑定
     */
    abstract public boolean onBindData(T oldData, T data);

    /**
     * 异步加载数据
     */
    protected void asyncBindData(T data) {
    }

    /**
     * 是否多选
     */
    protected void setCheckable(boolean checkable) {
    }

    /**
     * 是否被选中
     */
    public void setChecked(boolean checked) {
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (onCheckedChangeListener != null) {
            onCheckedChangeListener.onItemCheckChanged(adapter.getIndex(getData()), isChecked);
        }
    }

    public int getIndex() {
        return index;
    }
}
